#include"service_impl.h"
using namespace MC;
using namespace std;
Service_impl::Service_impl()
{
	m_dao=new Servicedao_file_impl;
	m_vecDepts=new vector<Department>;
	m_dao->load(*m_vecDepts);
}
void Service_impl::addDept(const Department& dp)
{
	m_vecDepts->push_back(dp);
}
bool Service_impl::deleteDept(int id)
{
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{
		if(id==m_vecDepts->at(i).m_nid)
		{
			vector<Department>::iterator p=m_vecDepts->begin();
			m_vecDepts->erase(p+i);
			return true;
		}
	}
	return false;

}
void Service_impl::listDept(void)
{
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{	
		cout<<m_vecDepts->at(i)<<endl;
	}
}
bool Service_impl::addEmp(int id,const Employee& em)
{
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{
		if(id==m_vecDepts->at(i).m_nid)
		{
			cout<<"++++++++++++++++++++"<<endl;
			cout<<m_vecDepts->at(i)<<endl;
			m_vecDepts->at(i).m_vecEmps.push_back(em);
			cout<<"------------------"<<endl;
			return true;
		}
	}
	return false;
}
bool Service_impl::deleteEmp(int id)
{
	bool flag=false;
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{
		if(m_vecDepts->at(i).deleteEmp(id))
		{
			flag=true;
		}
	}
	return flag;

}
bool Service_impl::modifyEmp(int id,const Employee& em)
{
	bool flag=false;
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{
		if(m_vecDepts->at(i).modifyEmp(id,em))
		{
			flag=true;
		}
	}
	return flag;
}
vector<Employee> Service_impl::listEmp(int id)
{
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{

		if(id==m_vecDepts->at(i).m_nid)
		{
			return m_vecDepts->at(i).m_vecEmps;
		}
	}
		
	cout<<"未找到该部门！"<<endl;
	return m_vecDepts->at(0).m_vecEmps;
}
void Service_impl::listAllEmp(void)
{
	for(unsigned int i=0;i<m_vecDepts->size();i++)
	{
		m_vecDepts->at(i).listEmp();
	}
	//return new_vecEmps;
}
Service_impl::~Service_impl()
{
	m_dao->save(*m_vecDepts);
	delete m_dao;
	delete m_vecDepts;
}
namespace MC
{
ostream& operator<<(ostream& out, Department& dp)
{
	
	 return out<<"name:"<<dp.m_strName<<"  "<<"id"<<dp.m_nid;
	 
	 
}
}
